package com.gyms.view.bottomview;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.classic.okhttp.beans.HVClickInfoBean;
import com.classic.okhttp.beans.HVHomePageItemBean;
import com.classic.okhttp.enums.Enum;
import com.gyms.R;
import com.gyms.constant.Constants;
import com.gyms.manager.BannerIntentManger;
import com.gyms.manager.StatisticsManager;

import java.util.HashMap;

import cache.ACache;
import constant.PicSplit;
import glide.ImageLoaderUtils;
import utils.SizeUtils;
import utils.StringUtils;

/**
 * Created by ltx on 17/6/5
 * 首页推荐弹框
 */

public class RecommendDialog extends Dialog implements View.OnClickListener, DialogInterface.OnDismissListener {
    private Context mContext;
    private ImageView mIvContent;//显示内容
    private ImageView mIvClose;//关闭按钮
    private HVHomePageItemBean mHVHomePageItemBean;
    private BannerIntentManger bannerIntentManger;//banner 的点击事件
    public RecommendDialog(@NonNull Context context) {
        super(context,R.style.dialog_recommend);
        this.mContext=context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //点击不能返回
        setCanceledOnTouchOutside(false);
        initView();
        initData();
        setListener();
        doSetDialogSize();
    }

    private void initData() {
        bannerIntentManger=new BannerIntentManger(mContext);
    }

    /**
     * 设置监听事件
     */

    private void setListener() {
        setCancelable(false);
        mIvClose.setOnClickListener(this);
        mIvContent.setOnClickListener(this);
        this.setOnDismissListener(this);
    }

    /**
     * 设置dialog框高
     */

    private void doSetDialogSize() {
        Window dialogWindow = getWindow();
        if(null==dialogWindow){
            return;
        }
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        int mDiglogWid=d.widthPixels- SizeUtils.dp2px(mContext,108);// 设置dialog宽度
        lp.width =mDiglogWid ;
        dialogWindow.setAttributes(lp);
        //设置ImageView的高度
        ViewGroup.LayoutParams params = mIvContent.getLayoutParams();
        params.width= mDiglogWid;
        params.height =mDiglogWid*SizeUtils.dp2px(mContext,356)/SizeUtils.dp2px(mContext,267);
        mIvContent.setLayoutParams(params);
    }

    private void initView() {
        View view = View.inflate(mContext,R.layout.recommend_dialog,null);
        setContentView(view);
        mIvContent= (ImageView) findViewById(R.id.iv_recommend_content);
        mIvClose= (ImageView) findViewById(R.id.iv_recommend_close);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_recommend_close://关闭
                dismiss();
                break;

            case R.id.iv_recommend_content://点击详情
                doBannerClick(mHVHomePageItemBean.getClickInfo());
                dismiss();
                break;
        }
    }

    /**
     * 设置首页弹窗的ItemBean
     * @param HVHomePageItemBean HVHomePageItemBean
     */

    public void setHVHomePageItemBean(HVHomePageItemBean HVHomePageItemBean) {
        mHVHomePageItemBean = HVHomePageItemBean;
        doFullDialog();
    }

    /**
     * 填充图片
     */

    private void doFullDialog() {
        String mPicStr=mHVHomePageItemBean.getPic();
        if(StringUtils.isNull(mPicStr)){
            return;
        }
        ImageLoaderUtils.doLoadRoundFillet(mContext, PicSplit.urlAdd(mPicStr,PicSplit.TYPE_HOME_BANNER_UP),mIvContent,5);
    }

    /**
     * banner 点击
     * @param getClickInfo 点击类
     */

    private void doBannerClick(HVClickInfoBean getClickInfo) {
        //处理图片的点击事件 一般是跳转到广告的详情界面
        if (!StringUtils.isNull(getClickInfo)) {
            Enum.EnumHomepageAction action = getClickInfo.getAction();
            String key = StatisticsManager.HomePage_banner;
            HashMap<String, String> map = new HashMap<>();
            map.put("bannerId", getClickInfo.getBannerId() + "");
            StatisticsManager.onEvent(mContext, key, map);

            if (!StringUtils.isNull(getClickInfo.getUrl())) {
                String[] gymIds = getClickInfo.getUrl().split(";");
                if (gymIds.length > 0) {
                    bannerIntentManger.bannerIntent(getClickInfo,action, getClickInfo.getTitle(), gymIds);
                }
            }
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {//dialog消失记录它的时间
        if(null==mHVHomePageItemBean||null==mHVHomePageItemBean.getClickInfo()){
            return;
        }
        int mBannerID = mHVHomePageItemBean.getClickInfo().bannerId;
        ACache.get(mContext).put(Constants.ACACHE_HOME_BANNNER,String.valueOf(mBannerID), ACache.THREEE_TIME_DAY);
    }
}
